package cn.lyvust.shili01;

import javax.swing.*;
import java.awt.*;

/**
 * Description: 演示网格布局管理器(GridLayout)
 * https://blog.csdn.net/xietansheng/article/details/72814492
 * date: 2021-6-23 18:53
 *
 * @author Guo_guo
 * @since JDK 1.8
 */
public class GridLayoutTest extends JFrame {
    public GridLayoutTest() {
        this.init();
        this.setTitle("演示网格布局");
        this.setSize(400, 250);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
    }

    private void init() {
        //创建 3 行 3 列 的网格布局, 并 设置 水平 和 竖直 间隙为10
        GridLayout layout = new GridLayout(3, 3, 10, 10);
        // 创建一个容器组件的面板
        JPanel panel = new JPanel(layout);
        JButton btn01 = new JButton("按钮01");
        JButton btn02 = new JButton("按钮02");
        JButton btn03 = new JButton("按钮03");
        JButton btn04 = new JButton("按钮04");
        JButton btn05 = new JButton("按钮05");
        JButton btn06 = new JButton("按钮06");
        JButton btn07 = new JButton("按钮07");
        JButton btn08 = new JButton("按钮08");
        JButton btn09 = new JButton("按钮09");

        panel.add(btn01);
        panel.add(btn02);
        panel.add(btn03);
        panel.add(btn04);
        panel.add(btn05);
        panel.add(btn06);
        panel.add(btn07);
        panel.add(btn08);
        panel.add(btn09);

        this.setContentPane(panel);

    }

    public static void main(String[] args) {
        GridLayoutTest gridLayoutTest = new GridLayoutTest();
    }
}
